﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BHan.Util
{
    public class EnumConverterUtil
    {
        /// <summary>
        /// 获取枚举项的字符串集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<string> EnumToList<T>() where T : Enum
        {
            // 使用Enum.GetValues()获取枚举的所有值  
            // 然后使用Cast<T>()将其转换为IEnumerable<T>，最后使用ToList()转换为List<T>  
            return Enum.GetNames(typeof(T)).ToList();
        }

        /// <summary>
        /// 将 id 转换为对应的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string IdToValue<T>(int id)
        {
            string value = Enum.GetName(typeof(T), id);
            return value;
        }

        /// <summary>
        ///   将 id 转换为对应的 枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T IdToObject<T>(int id)
        {
            T obj = (T)Enum.ToObject(typeof(T), id);
            return obj;
        }

        /// <summary>
        /// 将 char 转换为对应的 枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T IdToObject<T>(char id)
        {
            int value = -1;
            int.TryParse(id.ToString(), out value);
            T obj = (T)Enum.ToObject(typeof(T), value);
            if (obj == null)
            {
                return default;
            }
            return obj;
        }



        /// <summary>
        /// 将描述转换为 true 和 false
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool ValueToBool<T>(string value)
        {
            int val = (int)Enum.Parse(typeof(T), value);
            if (val == 1)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 将true 和 false 转换为 描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string BoolToValue<T>(bool id)
        {
            string value = Enum.GetName(typeof(T), id);

            return value;
        }

        /// <summary>
        ///  将描述转换为对应的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ValueToInt<T>(string value)
        {

            int val = (int)Enum.Parse(typeof(T), value);
            return val;
        }

        /// <summary>
        /// 通过枚举值获取描述  
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string EnumToDescription<T>(T value) where T : Enum
        {
            string description = value.ToString(); // 默认返回枚举名称  

            // 获取枚举成员的DescriptionAttribute  
            MemberInfo[] memberInfo = typeof(T).GetMember(value.ToString());
            if (memberInfo != null && memberInfo.Length > 0)
            {
                object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs != null && attrs.Length > 0)
                {
                    // 提取描述  
                    description = ((DescriptionAttribute)attrs[0]).Description;
                }
            }

            return description;
        }

        // 通过整数值获取描述  
        public static string IntToEnumDescription<TEnum>(int value) where TEnum : Enum
        {
            TEnum enumValue = IntToEnum<TEnum>(value);
            return EnumToDescription(enumValue);
        }


        // 辅助方法：通过整数值获取枚举成员  
        private static TEnum IntToEnum<TEnum>(int value) where TEnum : Enum
        {
            return (TEnum)Enum.ToObject(typeof(TEnum), value);
        }


    }
}
